//***************************************************************************
//*
//*	File:			dswl_genericproject.cpp
//*	Description:	Wrapper for IGenericProject interface
//*
//***************************************************************************

//
//--------------------------------------------------------------- PRECOMPILED
//

#include "stdafx.h"

//
//--------------------------------------------------- DECLARATION DEPENDENCIES
//

//
//--------------------------------------------------------------- DECLARATION
//

#include "../Inc/dswl_genericproject.h"

//
//--------------------------------------------------- DEFINITION DEPENDENCIES
//

#include	"Shlwapi.h"
#pragma		message ( "dswl_genericproject.cpp \tAuto-Linking with: Shlwapi.lib" )
#pragma		comment ( lib, "Shlwapi.lib" )

//
//-------------------------------------------------------------- PREPROCESSOR
//

#ifdef	_DEBUG
#define new DEBUG_NEW
#undef	THIS_FILE
static	char THIS_FILE[] = __FILE__;
#endif

//
//---------------------------------------------------------------- DEFINITION
//

/*
static void g_TestCasts()
{
	// test _safe_ conversions
	IGenericProject* pITest;
	CIGenericProject test	 ( pITest );
	CIGenericProject test2 = pITest ;

	test = test2;
	test = pITest;

	#ifdef _DSWL_SWITCH_USE_AUTOCAST

	// test _critical_ conversions
	pITest = test;
	CComPtr< IGenericProject > tmplTest = test;

	#endif
}
*/

//
//---------------------------------------------------------------------------------------------------
//*************************************     CON/DESTRUCTION     *************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																					  Constructor
//***************************************************************************************************
//**	@DOC		CONSTRUCTION
//**	@MFUNC		Default constructor
//**	@PARM		[in] A pointer to the interface to attach to
//**	@END
//***************************************************************************************************
//inline
CIGenericProject::CIGenericProject( IGenericProject* a_pIGenericProject /*= NULL*/ )
{
	this->m_pIGenericProject = a_pIGenericProject;
}

//***************************************************************************************************
//**																					   Destructor
//***************************************************************************************************
//**	@DOC		CONSTRUCTION
//**	@MFUNC		Default destructor
//**	@END
//***************************************************************************************************
//inline
CIGenericProject::~CIGenericProject()
{
	this->m_pIGenericProject = NULL;	// explicitly release the interface now
}

//
//---------------------------------------------------------------------------------------------------
//***************************************     PROPERTIES     ****************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																				   GetApplication 
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Determines the application this project belongs to
//**	@RDESC		A pointer to the application's interface
//**	@END
//***************************************************************************************************
//inline
CIApplication CIGenericProject::GetApplication( void ) const 
{
	//
	//	CHECK VALIDITY
	//
	if ( FALSE == this->IsValid() )
	{
		return NULL;
	}

	//
	//	DELEGATE
	//
	IDispatch*	pIDispatch = NULL;

	DSWL_VERIFY( this->m_pIGenericProject->get_Application( &pIDispatch ) );

	//
	//	QUERY INTERFACE
	//
	CComQIPtr< IApplication, &IID_IApplication > pIApplication ( pIDispatch );	

	//
	//	REPELASE DISPATCH INTERFACE
	//
	DSWL_RELEASE_EX( pIDispatch, CIGenericProject::GetApplication );

	//
	//	RETURN QUERIED INTERFACE
	//
	return pIApplication.p;												
}

//***************************************************************************************************
//**																					  GetFullName 		
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Returns the project's full name
//**	@RDESC		The project's full name
//**	@END
//***************************************************************************************************
//inline
CString CIGenericProject::GetFullName( void ) const 
{
	ASSERT( TRUE == this->IsValid() );

	//
	//	DELEGATE
	//
	CComBSTR bszFullName( _T("") );

	DSWL_VERIFY( this->m_pIGenericProject->get_FullName( &bszFullName ) );

	return bszFullName;
}

//***************************************************************************************************
//**																						  GetName 		
//***************************************************************************************************
//**	@DOC		PROPERTIES
//**	@MFUNC		Returns the project's name
//**	@RDESC		The project's name
//**	@END
//***************************************************************************************************
//inline
CString CIGenericProject::GetName( void ) const 
{
	ASSERT( TRUE == this->IsValid() );

	//
	//	DELEGATE
	//
	CComBSTR bszName( _T("") );

	DSWL_VERIFY( this->m_pIGenericProject->get_Name( &bszName ) );

	return bszName;
}

//
//---------------------------------------------------------------------------------------------------
//***************************************     INFORMATION     ***************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																						   IsNull
//***************************************************************************************************
//**	@DOC		INFORMATION
//**	@MFUNC		Determines whether the internal pointer is <t NULL>
//**	@RDESC		<t TRUE> if the internal pointer is <t NULL>, <t FALSE> otherwise.
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericProject::IsNull( void ) const
{
	return ( NULL == this->m_pIGenericProject.p );
}

//***************************************************************************************************
//**																						  IsValid
//***************************************************************************************************
//**	@DOC		INFORMATION
//**	@MFUNC		Determines whether the internal pointer differs from <t NULL>
//**	@RDESC		<t TRUE> if the internal pointer differs from <t NULL>, <t FALSE> otherwise.
//**	@END
//***************************************************************************************************
//inline
BOOL CIGenericProject::IsValid( void ) const
{
	return ( NULL != this->m_pIGenericProject.p );
}

//
//---------------------------------------------------------------------------------------------------
//***************************************     EXTENSIONS     ****************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																			   GetSourceFileNames
//***************************************************************************************************
//**	@DOC		EXTENSIONS
//**	@MFUNC		Determines the paths of all source files contained in this project
//**	@PARM		[out] An array to fill with full file paths to all source files
//**	@RDESC		The number of files added
//**	@END
//***************************************************************************************************
//inline
long CIGenericProject::GetSourceFilePaths( CFilePathArray& a_arrPaths ) const	
{
	ASSERT( TRUE == this->IsValid() );

	//
	//	SETUP VARS
	//
	CString strFullName = this->GetFullName();
	CString strName		= this->GetName();
	long	lFiles		= 0;

	if ( ".dsp" != strFullName.Right( 4 ) )
	{
		//
		//	UNSUPPORTED PROJECT TYPE
		//
		::MessageBox( NULL, _T( "Could not recognize project file format!" ), _T( "DSWrap" ), MB_ICONERROR );

		return 0;
	}

	//
	//	DETERMINE PATH
	//
	CString	strPath		= strFullName.Left( strFullName.GetLength() - strName.GetLength() - 4 );	// strip off "<name>.dsp"

	//
	//	OPEN PROJECT FILE FOR READ ACCESS
	//
	CStdioFile	fileIn	( strFullName, CFile::modeRead | CFile::typeText );
	CString		strLine	;
	CString		strFile	;
	CString		strTemp	;

	//
	//	READ LINES
	//
	while( TRUE == fileIn.ReadString( strLine ) )	
	{
		//
		//	CHECK FOR SOURCE FILE LINE
		//
		if ( 0 != strLine.Find( "SOURCE=" ) )
		{
			continue;
		}

		//
		//	PARSE FILE NAME
		//
		::sscanf( strLine.GetBuffer( 2 * _MAX_PATH ), "SOURCE=%s", strFile.GetBuffer( _MAX_PATH ) );

		strLine.ReleaseBuffer();
		strFile.ReleaseBuffer();

		//
		//	GENERATE FILE PATH
		//
		strTemp = strPath + strFile;

		::PathCanonicalize( strFile.GetBuffer( 2 * _MAX_PATH ), strTemp );

		strFile.ReleaseBuffer();

		//
		//	ADD FILE TO ARRAY
		//
		a_arrPaths.Add( strFile );

		//
		//	INCREASE COUNTER
		//
		++lFiles;
	}

	//
	//	CLOSE FILE
	//
	fileIn.Close();

	//
	//	RETURN RESULT
	//
	return lFiles;
}

//
//---------------------------------------------------------------------------------------------------
//****************************************     OPERATORS     ****************************************
//---------------------------------------------------------------------------------------------------
//

//***************************************************************************************************
//**																					  operator ==
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic project to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericProject::operator == ( const CIGenericProject& a_Other ) const 
{
	return this->m_pIGenericProject == a_Other.m_pIGenericProject;
}

//***************************************************************************************************
//**																					  operator !=
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic project to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericProject::operator != ( const CIGenericProject& a_Other ) const 
{
	return this->m_pIGenericProject != a_Other.m_pIGenericProject;
}

//***************************************************************************************************
//**																					  operator <=
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic project to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericProject::operator <= ( const CIGenericProject& a_Other ) const 
{
	return this->m_pIGenericProject.p <= a_Other.m_pIGenericProject.p;
}

//***************************************************************************************************
//**																					  operator >=
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic project to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericProject::operator >= ( const CIGenericProject& a_Other ) const 
{
	return this->m_pIGenericProject.p >= a_Other.m_pIGenericProject.p;
}

//***************************************************************************************************
//**																					   operator <
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic project to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericProject::operator < ( const CIGenericProject& a_Other ) const 
{
	return this->m_pIGenericProject.p < a_Other.m_pIGenericProject.p;
}

//***************************************************************************************************
//**																					   operator >
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Comparison operator
//**	@PARM		[in] The generic project to compare to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
bool CIGenericProject::operator > ( const CIGenericProject& a_Other ) const 
{
	return this->m_pIGenericProject.p > a_Other.m_pIGenericProject.p;
}

//***************************************************************************************************
//**																					   operator =
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Copy operator
//**	@PARM		[in] The generic project to attach to
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
CIGenericProject& CIGenericProject::operator = ( IGenericProject* a_pIOther ) 
{
	this->m_pIGenericProject = a_pIOther;

	return (*this);
}

//***************************************************************************************************
//**																					   operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Dereference operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
IGenericProject* CIGenericProject::operator * ( void ) 
{
	return this->m_pIGenericProject.p;
}

//***************************************************************************************************
//**																				 const operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Dereference operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
const IGenericProject* CIGenericProject::operator * ( void ) const 
{
	return this->m_pIGenericProject.p;
}

//***************************************************************************************************
//**																					  operator ->
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Member access operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
IGenericProject* CIGenericProject::operator -> ( void ) 
{
	return * (*this);
}

//***************************************************************************************************
//**																				const operator ->
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		Member access operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
const IGenericProject* CIGenericProject::operator -> ( void ) const 
{
	return * (*this);
}

//---------------------------------------------------------------------------------------------------
//***************************************************************************************************
//---------------------------------------------------------------------------------------------------

#ifdef _DSWL_SWITCH_USE_AUTOCAST
#ifdef _DSWL_SWITCH_USE_UNSAFE_AUTOCAST

//***************************************************************************************************
//**																				 const operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		TypeCast operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
CIGenericProject::operator IGenericProject* ( void ) const 
{
	return this->m_pIGenericProject.p;
}

#else

//***************************************************************************************************
//**																					   operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		TypeCast operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
CIGenericProject::operator IGenericProject* ( void ) 
{
	return this->m_pIGenericProject.p;
}

//***************************************************************************************************
//**																				 const operator *
//***************************************************************************************************
//**	@DOC		OPERATORS
//**	@MFUNC		TypeCast operator
//**	@RDESC		
//**	@END
//***************************************************************************************************
//inline
CIGenericProject::operator const IGenericProject* ( void ) const 
{
	return this->m_pIGenericProject.p;
}

#endif
#endif


